<?php
namespace zhtfrontend\components\services\goods;
use common\components\helpers\Fun;
use EasyWeChat\Server\BadRequestException;
use zhtfrontend\models\datas\ArtTeaching;
use zhtfrontend\models\datas\ClassManage;
use zhtfrontend\models\datas\ClassManageGoods;
use zhtfrontend\models\datas\Goods;
use zhtfrontend\models\datas\GoodsAttr;
use yii\base\Action;
use yii\web\Response;
use yii\helpers\ArrayHelper;
use yii\web\NotFoundHttpException;
use zhtfrontend\models\datas\GoodsGroup;
use zhtfrontend\models\datas\GoodsPrice;


class ShelveAction extends Action{
    public $type;
    public function run(){
        if(\Yii::$app->request->isAjax){
            \Yii::$app->response->format = Response::FORMAT_JSON;
            $return = ['status'=>0,'content'=>'','is_shelve'=>0];
            if(\Yii::$app->request->post('id') == null){
                $return['content'] = '系统错误刷新页面重试！';
                return $return;
            }
            $id=\Yii::$app->request->post('id');
            $goods = Goods::findOne($id);
            if($goods==null){
                $return['content'] = '商品不存在！';
                return $return;
            }

            $price = GoodsPrice::find()->where(['goods_id'=>$id,'status'=>1])->all();
            if($price == null){
                $return['content'] = '请先设置保证金与价格套餐！';
            }elseif ($goods->is_update_price == 1){
                $return['content'] = '请先提报或审核中！';
            } else{
                if($goods->is_shelve==0){
                    $goods->is_shelve=1;
                    $goods->up_at=time();
                    $goods->save(false);
                    $return['status'] = 1;
                    $return['is_shelve'] = 1;
                }else{
                    $goods->is_shelve=0;
                    $goods->down_at=time();
                    $goods->save(false);
                    $return['status'] = 1;
                }
            }
            return $return;
            //上架
//            if($goods->is_shelve==0 && ($goods->deposit+$goods->admission_fee+$goods->cost)>0){
//                    $goods->is_shelve=1;
//                    $goods->up_at=time();
//                    if($goods->save(false)){
//                        $classmanage=ClassManage::findOne(['is_old'=>0,'associate_id'=>$goods->id,'type'=>1]);
//                        if($classmanage==null){
//                            $goods_attr=GoodsAttr::findOne(['goods_id'=>$goods->id,'obj_type'=>'is_booking']);
//                            $class_manage=new ClassManage();
//                            $class_manage->class_code='c'.time();
//                            $class_manage->is_book=$goods_attr!=null?(int)($goods_attr->value):null;
//                            $class_manage->is_old=0;
//                            $class_manage->associate_id=$goods->id;
//                            $class_manage->mechanic_id=$goods->mechanic_id;
//                            $class_manage->type=1;
//                            $class_manage->save(false);
//                        }else{
//                           $classmanage->is_delete = 0;
//                           $classmanage->save(false);
//                        }
//
//                        if($group != null){
//                            $groupManage = ClassManage::findOne(['is_old'=>0,'associate_id'=>$group->group_id,'type'=>2]);
//                            if($groupManage == null){
//                                $artModel = ArtTeaching::findOne($group->group_id);
//                                $groupManage = new ClassManage();
//                                $groupManage->class_code='c'.time();
//                                $groupManage->is_book=1;
//                                $groupManage->associate_id=$group->group_id;
//                                $groupManage->mechanic_id=$artModel->mechainc_id;
//                                $groupManage->type=2;
//                                $groupManage->save(false);
//                            }else{
//                                $groupManage->is_delete = 0;
//                                $groupManage->save(false);
//                            }
//                            if($classmanage->child_id == 0) {
//                                $class = ClassManage::findOne($classmanage->class_id);
//                                $class->child_id = $groupManage->class_id;
//                                $class->save(false);
//                            }
//                        }
//
//                    }
//                $return['status'] = 1;
//                $return['is_shelve'] = 1;
//            } elseif($goods->is_shelve==0 && ($goods->deposit+$goods->admission_fee+$goods->cost) <=0){
//                $return['content'] = '请先设置保证金、招生费、招生费！';
//            } elseif($goods->is_shelve==1){
//                $goods->is_shelve=0;
//                $goods->down_at=time();
//                $goods->save(false);
//                $return['status'] = 1;
//            }
           return $return;
        }
    }

//
//    public function check_can_shelve($goods_id){
//        $info_id=GoodsAttr::find()->where(['goods_id'=>$goods_id,'obj_type'=>'start_time'])->one(Fun::getSiteDb());
//        $goods_start_time=GoodsAttr::find()->select('goods_id')->where(['attribute_id'=>$info_id->attribute_id,'obj_type'=>'start_time','value'=>$info_id->value])->column(Fun::getSiteDb());
//        if(empty($goods_start_time)){
//            return true;
//        }else{
//            $info=GoodsAttr::find()->where(['goods_id'=>$goods_id])->asArray()->all(Fun::getSiteDb());
//            $info_array=ArrayHelper::map($info,'obj_type','value');
////            var_dump($info_array);die;
//            $goods_end_time=GoodsAttr::find()->select('goods_id')->where(['in','goods_id',$goods_start_time])->andWhere(['attribute_id'=>$info_id->attribute_id,'obj_type'=>'end_time','value'=>$info_array['end_time']])->column(Fun::getSiteDb());
//            if(empty($goods_end_time)){
//                return true;
//            }else{
//                $goods_class_day=GoodsAttr::find()->select('goods_id')->where(['in','goods_id',$goods_end_time])->andWhere(['attribute_id'=>$info_id->attribute_id,'obj_type'=>'class_day','value'=>$info_array['class_day']])->column(Fun::getSiteDb());
//                if(empty($goods_class_day)){
//                    return true;
//                }else{
//                    $goods=Goods::find()->where(['in','id',$goods_class_day])->andWhere(['is_delete'=>0,'is_shelve'=>1])->one(Fun::getSiteDb());
//                    if($goods==null){
//                        return true;
//                    }else{
//                        return false;
//                    }
//                }
//            }
//        }
//    }
}
?>
